package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.db.mapper.SmsFlashPromotionProductRelationMapper;
import com.mall.db.mapper.SmsFlashPromotionSessionMapper;
import com.mall.domain.SmsFlashPromotionProductRelation;
import com.mall.domain.SmsFlashPromotionSession;
import com.mall.interface1.api.SmsFlashPromotionSessionApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionSessionApiImpl implements SmsFlashPromotionSessionApi {

    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    //添加场次
    public void create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.insert(smsFlashPromotionSession);
    }

    //根据id删除场次
    public void deleteById(Long id) {
        smsFlashPromotionSessionMapper.deleteById(id);
    }

    //获取全部场次
    public List<SmsFlashPromotionSession> list() {
        return smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<>());
    }

    //获取所有的FlashSession数据
    public List<SmsFlashPromotionSession> selectSession() {
        return smsFlashPromotionSessionMapper.selectList(null);
    }

    //根据条件获得活动对应产品数量
    public Integer selectCount(Long flashPromotionId, Long sessionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        queryWrapper.eq(sessionId != null,SmsFlashPromotionProductRelation::getFlashPromotionSessionId,sessionId);
        return smsFlashPromotionProductRelationMapper.selectCount(queryWrapper);
    }

    //根据id获取活动
    public SmsFlashPromotionSession findById(Long id) {
        return smsFlashPromotionSessionMapper.selectById(id);
    }

    //修改状态
    public void updateStatus(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.updateById(smsFlashPromotionSession);
    }

    //修改场次
    public void update(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.updateById(smsFlashPromotionSession);
    }

}
